using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;

using SypGui.CreateForms;
using SypConst.Garbage;

namespace SypGui.OptionForms
{
    public partial class OptionMainPanel : SypUserControl
    {
        private SypPrefForm.SypPrefStatusText StatusBarText = null;

        protected override void InitializeRessources()
        {
            OptionPanelButtonBrowseNavigator.Text = GetRessources("OptionBrowseButton");
            OptionPanelLabelPort.Text = GetRessources("OptionPort");
            OptionPanelLabelPublishing.Text = GetRessources("OptionPublishingFrequency");
            OptionPanelGroupBox.Text = GetRessources("OptionGeneralSettings");
            OptionPanelNotificationCheckbox.Text = GetRessources("OptionPanelNotification");
            OptionPanelLanguageEnglish.Text = GetRessources("OptionENLanguage");
            OptionPanelLanguageFrench.Text = GetRessources("OptionFRLanguage");
            OptionPanelLabelNavigator.Text = GetRessources("OptionNavigator");
            OptionPanelCheckboxAutostart.Text = GetRessources("OptionAutoLaunch");
            OptionPanelLanguageLabel.Text = GetRessources("OptionLanguage");
        }

        public OptionMainPanel()
            : base (){}

        public override void ProcessConfig()
        {
            SypGarbageConf myConf = SypGarbageConf.Instance();
            int port = SypGarbage.SYP_DEFAULT_PORT;
            if (!int.TryParse(OptionPanelTextboxPort.Text, out port))
            {
                StatusBarText("Le port saisi est incorrect");
                return;
            }
            myConf.SypServerPort = port;
            myConf.WindowsAutoStart = OptionPanelCheckboxAutostart.Checked;
            myConf.PublishingIntervalMin =  int.Parse(OptionPanelPublishingintervalMin.Value.ToString());
            myConf.PublishingIntervalSec = int.Parse(OptionPanelPublishingintervalSec.Value.ToString());
            myConf.NotificationEnable = OptionPanelNotificationCheckbox.Checked;


         //   myConf.SynchronisingIntervalMin = int.Parse(OptionPanelSyncintervalMin.Value.ToString());
         //   myConf.SynchronisingIntervalSec = int.Parse(OptionPanelSyncintervalSec.Value.ToString());

            if (File.Exists(OptionPanelBrowserTextBox.Text))
                myConf.SypDefaultBrowser = OptionPanelBrowserTextBox.Text;
            else
                StatusBarText("Unkown browser file...");
            if (OptionPanelLanguageFrench.Checked)
                myConf.Language = SypGarbage.SYP_CULTURE_FRENCH;
            else
                myConf.Language = SypGarbage.SYP_CULTURE_US;
        }

        public void RefreshConfig()
        {
            SypGarbageConf myConf = SypGarbageConf.Instance();
            if (myConf != null)
            {
                OptionPanelTextboxPort.Text = myConf.SypServerPort.ToString();
                OptionPanelCheckboxAutostart.Checked = myConf.WindowsAutoStart;
                OptionPanelBrowserTextBox.Text = myConf.SypDefaultBrowser;

                OptionPanelPublishingintervalMin.Value = myConf.PublishingIntervalMin;
                OptionPanelPublishingintervalSec.Value = myConf.PublishingIntervalSec;
                OptionPanelNotificationCheckbox.Checked = myConf.NotificationEnable;
                //OptionPanelSyncintervalMin.Value = myConf.SynchronisingIntervalMin;
                //OptionPanelSyncintervalSec.Value = myConf.SynchronisingIntervalSec;
                if (myConf.Language.Equals(SypGarbage.SYP_CULTURE_FRENCH))
                {
                    OptionPanelLanguageFrench.Checked = true;
                }
                else
                {
                    OptionPanelLanguageEnglish.Checked = true;
                }

            }
        }


        private void InitPanel()
        {
            Location = new System.Drawing.Point(120, 12);
            Name = "optionMainPanel";
            Size = new System.Drawing.Size(361, 270);
            TabIndex = 2;
        }

        public OptionMainPanel(SypPrefForm.SypPrefStatusText myStatusBar, SetText outputMethod)
            : base(outputMethod)
        {
            StatusBarText = myStatusBar;
            InitPanel();
            RefreshConfig();
        }

        private void OptionPanelButtonBrowseNavigator_Click(object sender, EventArgs e)
        {
            if (OptionPanelFileDialog.ShowDialog() != DialogResult.OK)
            {
                OptionPanelBrowserTextBox.Text = "";
                StatusBarText("Vous devez renseigner obligatoirement un navigateur.");
                Directory.SetCurrentDirectory(Application.StartupPath);
                return;
            }
            else
            {
                OptionPanelBrowserTextBox.Text = OptionPanelFileDialog.FileName;
                StatusBarText(GetRessources("OptionsStatusSettingsApplied"));
                Directory.SetCurrentDirectory(Application.StartupPath);
            }
        }

        private void OptionPanelCheckboxAutostart_Click(object sender, EventArgs e)
        {
            if (OptionPanelCheckboxAutostart.Checked)
                SypGarbageConf.SetAutoStart();
            else
                SypGarbageConf.RemoveAutoStart();
        }

        private void OptionPanelLanguageEnglish_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelLanguageFrench_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelCheckboxAutostart_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsApplied"));
        }

        private void OptionPanelPublishingintervalMin_ValueChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelPublishingintervalSec_ValueChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelSyncintervalSec_ValueChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelSyncintervalMin_ValueChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsChange"));
        }

        private void OptionPanelNotificationCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            StatusBarText(GetRessources("OptionsStatusSettingsApplied"));
        }

        
    }
}
